package Lock;

public class DeadLock {

    public static void main(String[] args) {
        A a = new A();
        B b = new B();

        new Thread(new Runnable() {
            @Override
            public void run() {
                a.Methon_A(b);
            }
        }).start();

        new Thread(new Runnable() {
            @Override
            public void run() {
                b.Methon_B(a);
            }
        }).start();

    }

}

class A {
    public synchronized void Methon_A(B b) { // 需要拿到Object A的锁
        System.out.println("当前线程名: " + Thread.currentThread().getName() + " 进入了A实例的Methon_A方法");
        try {
            Thread.sleep(200);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("当前线程名: " + Thread.currentThread().getName() + " 企图调用B实例的last方法");
        b.last();
    }

    public synchronized void last() { // 需要拿到Object A的锁
        System.out.println("调用了A类的last方法内部");
    }
}

class B {
    public synchronized void Methon_B(A a) { // 需要拿到Object B的锁
        System.out.println("当前线程名: " + Thread.currentThread().getName() + " 进入了B实例的Methon_B方法");
        try {
            Thread.sleep(200);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("当前线程名: " + Thread.currentThread().getName() + " 企图调用A实例的last方法");
        a.last();
    }

    public synchronized void last() { // 需要拿到Object B的锁
        System.out.println("调用了B类的last方法内部");
    }
}
